﻿<%@ WebHandler Language="C#" Class="SyncGigHandler" %>

using System;
using System.Web;
using System.Diagnostics;
using System.IO;

public class SyncGigHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        System.Diagnostics.Process pro = new System.Diagnostics.Process();
        string me = "";
        string batPath = "请别乱执行，谢谢！";
        if (context.Request.QueryString["me"] == null)
        {
            context.Response.Write(batPath);
            return;
        }
        else
        {
            me = context.Request.QueryString["me"].ToString();
        }
        if (me == "me")
        {
            batPath = "D:\\bak\\执行发布操作.bat";
        }
        else if (me == "szlgchild")
        {
            batPath = "E:\\site\\bak\\执行发布操作.bat";
        }
        else
        {
            context.Response.Write(batPath);
            return;
        }
        string eb = ExecuteBAT(batPath, pro);
        //string eb = GetTest(batPath);
        context.Response.Write(eb);          
    }
    private string ExecuteBAT(string strBatPath, Process pro)
    {
        string mess = "";
        try
        {
            pro.StartInfo.UseShellExecute = true;
            pro.StartInfo.FileName = strBatPath;  //strBatPath是bat文件路径
            pro.StartInfo.CreateNoWindow = true;
            if (pro.Start())
            {
                mess = DateTime.Now.ToLongDateString() + "执行成功.";//写日志
            }
            else
            {
                mess = string.Format("执行{0}失败.", strBatPath);
            }
        }
        catch (Exception ex)
        {
            mess = ex.Message;
        }
        finally
        {
            pro.Close();
        }
        return mess;
    }

    private string GetTest(string strBatPath)
    {
        if (File.Exists(strBatPath))
        {
            Process consoleProcess = new Process();

            //获取文件路径 比如 c:\PS\app0.bat,   
            string batFilePath = strBatPath;

            //我想让程序执行这个bat文件，停某个服务，app0.bat文件内容为：net stop appMgmt
            consoleProcess.StartInfo.FileName = batFilePath;
            consoleProcess.StartInfo.UseShellExecute = false;
            consoleProcess.StartInfo.RedirectStandardInput = true;
            consoleProcess.StartInfo.RedirectStandardOutput = true;

            consoleProcess.Start();
            StreamReader streamReader = consoleProcess.StandardOutput;
            return streamReader.ReadToEnd();//输出结果
        }
        else
        {
            return "文件不存在";
        }
    }



    private string GetTest1(string strBatPath)
    {
        Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = " cmd.exe ";
        startInfo.Arguments = "/c '" + strBatPath + "'";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = false;
        startInfo.RedirectStandardOutput = false;
        startInfo.CreateNoWindow = true;
        process.StartInfo = startInfo;
        string mess;
        //process.Start();
        if (process.Start())
        {
            mess = DateTime.Now.ToLongDateString() + "执行成功.";//写日志
        }
        else
        {
            mess = string.Format("执行{0}失败.", strBatPath);
        }
        process.WaitForExit();
        process.Dispose();
        return mess;
    }
    
    public bool IsReusable {
        get {
            return false;
        }
    }

}